function splitArray(arr, num) {
    // 确保 arr 是数组，num 是数字
    if (!Array.isArray(arr) || typeof num !== 'number' || num <= 0) {
        return [];
    }

    arr = arr.flat(Infinity)

    let result = [];
    // 遍历数组，将其按 num 长度切分成多个子数组
    for (let i = 0; i < arr.length; i += num) {
        result.push(arr.slice(i, i + num));
    }
    return result;
}

// 生成分页器函数
function paginateArray(arr, pageSize, currentPage) {
    // 计算当前页的起始索引和结束索引
    const start = (currentPage - 1) * pageSize;
    const end = start + pageSize;
    
    // 使用 slice() 方法切割数组并返回当前页的数据
    let list = arr.slice(start, end);
    if (!list.length) {
        uni.showToast({
            title:'数据加载完毕',
            icon:'none',
            duration:3000  
        })
        return []
    }
    return list
}

// 商品详情页面 table 表格生气

function getTableData(list) {
    let tableData = []

    let commodity = huohaoDetail
    let fitterData = []

    commodity.forEach((val) => {
        let data1 = []
        let chimaArr = val.data.chima === null ? [] : val.data.chima
        let colorArr = val.data.color === null ? [] : val.data.color

        if (!!colorArr) {
            colorArr.forEach((val1, index) => {
                let obj = {}
                obj.chima = []
                obj.color = val1
                chimaArr.forEach(item => {
                    let chimaobj = {}
                    chimaobj[item] = ''
                    obj.chima.push(chimaobj)
                })
                data1.push(obj)
            })
                chimaArr.unshift("")
        }


        let huohaoObj = {}
        huohaoObj.bbreed = val.bbreed
        huohaoObj.kcsl = val.kcsl
        huohaoObj.description = val.description
        huohaoObj.data = data1
        huohaoObj.showTable = false // 控制 table 得 显示和隐藏
        fitterData.push(huohaoObj)
    })

    fitterData.forEach((val, index) => {
        let fhArr = commodity[index].data.kc
        val.data.forEach(item => {
            let hj = 0
            item.chima.forEach(chimaItem => {
            for (let key in chimaItem) {
                fhArr.forEach(v => {
                if (v.chima == key && v.color == item.color) {
                    chimaItem[key] = v.kcsl
                    hj += v.kcsl*1
                }
                })
            }

            })
            item.hj = hj
        })
    })
    tableData = fitterData
    return tableData
}

// table 表格数据 
// 数据 返回参考接口 https://mpro28.ywhzsoft.com/ChengFeiTest/getSearchCjReport.aspx?sdate=2024-09-01&edate=2025-01-11&huohao=&chuanghao=
let huohaoDetail = [
    {
        "description": "奥粒绒外套女",
        "bbreed": "5805",
        "khname": null,
        "kcsl": 444,
        "data": {
            "chima": [
                "L",
                "XL",
                "2XL",
                "3XL"
            ],
            "color": [
                "黑松露",
                "酒庄红",
                "糯米白",
                "紫菂test",
                "蜜糖橙",
                "西柚粉",
                "",
                "尺码总数"
            ],
            "kc": [
                {
                    "chima": "2XL",
                    "color": "",
                    "kcsl": 4
                },
                {
                    "chima": "2XL",
                    "color": "尺码总数",
                    "kcsl": 4
                },
                {
                    "chima": "L",
                    "color": "尺码总数",
                    "kcsl": 30
                },
                {
                    "chima": "L",
                    "color": "酒庄红",
                    "kcsl": 30
                },
                {
                    "chima": "XL",
                    "color": "",
                    "kcsl": 10
                },
                {
                    "chima": "XL",
                    "color": "尺码总数",
                    "kcsl": 410
                },
                {
                    "chima": "XL",
                    "color": "酒庄红",
                    "kcsl": 400
                }
            ],
            "jh": null,
            "fk": null,
            "wfk": null,
            "jhhj": null,
            "fkhj": null,
            "wfkhj": null
        }
    },
    {
        "description": "奥粒绒外套男",
        "bbreed": "5806",
        "khname": null,
        "kcsl": 3678,
        "data": {
            "chima": [
                "M",
                "L",
                "XL",
                "2XL",
                "3XL",
                "4XL"
            ],
            "color": [
                "黑松露",
                "藏青",
                "军绿",
                "",
                "酒庄红",
                "尺码总数"
            ],
            "kc": [
                {
                    "chima": "",
                    "color": "",
                    "kcsl": 234
                },
                {
                    "chima": "",
                    "color": "尺码总数",
                    "kcsl": 234
                },
                {
                    "chima": "2XL",
                    "color": "",
                    "kcsl": 11
                },
                {
                    "chima": "2XL",
                    "color": "尺码总数",
                    "kcsl": 1711
                },
                {
                    "chima": "2XL",
                    "color": "黑松露",
                    "kcsl": 500
                },
                {
                    "chima": "2XL",
                    "color": "酒庄红",
                    "kcsl": 1200
                },
                {
                    "chima": "3XL",
                    "color": "",
                    "kcsl": 11
                },
                {
                    "chima": "3XL",
                    "color": "尺码总数",
                    "kcsl": 111
                },
                {
                    "chima": "3XL",
                    "color": "黑松露",
                    "kcsl": 100
                },
                {
                    "chima": "4XL",
                    "color": "尺码总数",
                    "kcsl": 100
                },
                {
                    "chima": "4XL",
                    "color": "黑松露",
                    "kcsl": 100
                },
                {
                    "chima": "L",
                    "color": "",
                    "kcsl": 11
                },
                {
                    "chima": "L",
                    "color": "尺码总数",
                    "kcsl": 778
                },
                {
                    "chima": "L",
                    "color": "黑松露",
                    "kcsl": 512
                },
                {
                    "chima": "L",
                    "color": "酒庄红",
                    "kcsl": 255
                },
                {
                    "chima": "M",
                    "color": "尺码总数",
                    "kcsl": 221
                },
                {
                    "chima": "M",
                    "color": "黑松露",
                    "kcsl": 221
                },
                {
                    "chima": "XL",
                    "color": "",
                    "kcsl": 11
                },
                {
                    "chima": "XL",
                    "color": "尺码总数",
                    "kcsl": 523
                },
                {
                    "chima": "XL",
                    "color": "黑松露",
                    "kcsl": 500
                },
                {
                    "chima": "XL",
                    "color": "酒庄红",
                    "kcsl": 12
                }
            ],
            "jh": null,
            "fk": null,
            "wfk": null,
            "jhhj": null,
            "fkhj": null,
            "wfkhj": null
        }
    },
    {
        "description": "万粒绒外套女",
        "bbreed": "5800",
        "khname": null,
        "kcsl": 1655,
        "data": {
            "chima": [
                "L",
                "XL",
                "2XL",
                "3XL"
            ],
            "color": [
                "青柠绿",
                "黑松露",
                "原露米",
                "酒庄红",
                "银质粉",
                "尺码总数"
            ],
            "kc": [
                {
                    "chima": "2XL",
                    "color": "尺码总数",
                    "kcsl": 200
                },
                {
                    "chima": "2XL",
                    "color": "青柠绿",
                    "kcsl": 200
                },
                {
                    "chima": "3XL",
                    "color": "尺码总数",
                    "kcsl": 200
                },
                {
                    "chima": "3XL",
                    "color": "银质粉",
                    "kcsl": 200
                },
                {
                    "chima": "L",
                    "color": "尺码总数",
                    "kcsl": 255
                },
                {
                    "chima": "L",
                    "color": "黑松露",
                    "kcsl": 255
                },
                {
                    "chima": "XL",
                    "color": "尺码总数",
                    "kcsl": 1000
                },
                {
                    "chima": "XL",
                    "color": "酒庄红",
                    "kcsl": 600
                },
                {
                    "chima": "XL",
                    "color": "原露米",
                    "kcsl": 400
                }
            ],
            "jh": null,
            "fk": null,
            "wfk": null,
            "jhhj": null,
            "fkhj": null,
            "wfkhj": null
        }
    }
]
  

export default {
    splitArray,
    getTableData,
    paginateArray,
}